package com.wwh.furns.web;

import com.google.gson.Gson;
import com.wwh.furns.entity.Cart;
import com.wwh.furns.entity.CartItem;
import com.wwh.furns.entity.Furn;
import com.wwh.furns.service.FurnService;
import com.wwh.furns.service.impl.FurnServiceImpl;
import com.wwh.furns.utils.DataUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Albert
 * @date 2024/7/28-17:14
 * @describe 处理购物车相关请求
 */
public class CartServlet extends BasicServlet{
    FurnService furnService = new FurnServiceImpl();
    public void addItem(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //得到所添加的家居的id
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        //获取id对应的furn
        Furn furn = furnService.queryFurnById(id);
        //System.out.println("furn.name = " + furn.getName());
        //根据furn构建cartItem
        CartItem item = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());
        //从session获取cart
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addCartItem(item);

        //System.out.println("item.name = " + item.getName());

        //添加完毕后，返回添加家居所在的页面
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);

    }

    /**
     * 修改购物车
     * @param req
     * @param resp
     * @throws IOException
     */
    public void updateCount (HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        int count = DataUtils.parseInt(req.getParameter("count"), 1);

        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        Furn furn = furnService.queryFurnById(id);

        if(cart != null && furn.getStock() >= count){
            cart.updateCount(id, count);
        }

        //System.out.println("cart = " + cart);
        //修改完毕后，返回添加家居所在的页面
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

    /**
     * 删除购物车中指定商品
     * @param req
     * @param resp
     * @throws IOException
     */
    public void deleteItem (HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);

        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");

        if(cart != null){
            cart.deleteItem(id);
        }

        System.out.println("cart = " + cart);
        //删除完毕后，返回添加家居所在的页面
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

    /**
     * 清空购物车
     * @param req
     * @param resp
     * @throws IOException
     */
    public void clearCart (HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");

        if(cart != null){
            cart.clear();
        }

        //System.out.println("cart = " + cart);
        //清空完毕后，返回添加家居所在的页面
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

    /**
     * 处理ajax发送的添加购物车的请求
     * @param req
     * @param resp
     */
    public void addItemByAjax(HttpServletRequest req, HttpServletResponse resp) {
        //得到所添加的家居的id
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        //获取id对应的furn
        Furn furn = furnService.queryFurnById(id);
        //System.out.println("furn.name = " + furn.getName());
        //根据furn构建cartItem
        CartItem item = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());
        //从session获取cart
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addCartItem(item);

        //System.out.println("item.name = " + item.getName());

        //添加完毕后，返回json数据
        //使用Map转成Json返回
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("cartCount", cart.getTotalCount());
        Gson gson = new Gson();
        String resultJson = gson.toJson(resultMap);

        // 返回
        try {
            resp.getWriter().write(resultJson);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
